﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NetCoreCMS.Models;

namespace NetCoreCMS.Web.Areas.WebApi.Controllers
{
    public class User_Info_EditController : UserController
    {
        protected Userconfig userConfig;
        [Authorize]
        [HttpGet]
        public IActionResult Get(string nick_name,string sex,string birthday,string mobile,string weixin,string province,string city,string area,string address,string bankname,string banknumber )
        {
            //检查用户是否登录
            Users model = GetUserInfo();
            if (model == null)
            {
                return Ok(new { status = 0, msg = "用户尚未登录或已超时！" });
            }
            string email = "";
            string telphone = mobile;
            string qq = "";
            string msn = weixin;
            //检查昵称
            if (string.IsNullOrEmpty(nick_name))
            {
                return Ok(new { status = 0, msg = "请输入您的姓名昵称！" });
            }
            //检查省市区
            if (string.IsNullOrEmpty(province) || string.IsNullOrEmpty(city) || string.IsNullOrEmpty(area))
            {
                return Ok(new { status = 0, msg = "请选择您所在的省市区！" });
            }
            //检查手机，如开启手机注册或使用手机登录需要检查
            userConfig = Common.XMLConfig.Userconfig.loadConfig(_env.ContentRootPath);
            if (userConfig.regstatus == 2 || userConfig.mobilelogin == 1)
            {
                if (string.IsNullOrEmpty(mobile))
                {
                    return Ok(new { status = 0, msg = "请输入您的手机号码！" });
                }
                if (model.mobile != mobile && _users.ExistsMobile(mobile))
                {
                    return Ok(new { status = 0, msg = "该手机号码已被使用！" });
                }
            }

            //开始写入数据库
            model.nick_name = nick_name;
            model.sex = sex;
            DateTime _birthday;
            if (DateTime.TryParse(birthday, out _birthday))
            {
                model.birthday = _birthday;
            }
            model.email = email;
            model.mobile = mobile;
            model.telphone = telphone;
            model.qq = qq;
            model.bank_name = bankname;
            model.bank_number = banknumber;
            model.msn = msn;
            model.area = province + "," + city + "," + area;
            model.address = address;

            _users.Update(model);
            return Ok(new { status = 0, msg = "账户资料已修改成功！" });
        }
    }
}